package com.dnjn.mes.biz.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dnjn.mes.biz.domain.MesExceptPlanXref;
import com.dnjn.mes.biz.domain.MesResolvePlan;
import com.dnjn.mes.biz.service.IMesExceptPlanXrefService;
import com.dnjn.mes.common.core.controller.BaseController;
import com.dnjn.mes.common.core.domain.AjaxResult;
import com.dnjn.mes.common.core.domain.R;
import com.dnjn.mes.common.utils.StringUtils;
import com.dnjn.mes.common.utils.poi.ExcelUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 异常及解决方案关联表 前端控制器
 * </p>
 *
 * @author Amber
 * @since 2023-07-26
 */
@RestController
@RequestMapping("/biz/mes-except-plan-xref")
public class MesExceptPlanXrefController extends BaseController {

    @Autowired
    protected IMesExceptPlanXrefService baseService;

    @GetMapping("/get/{id}")
    public AjaxResult get(@PathVariable Long id) {
        return success(baseService.getById(id));
    }

    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody MesExceptPlanXref domain) {
        return toAjax(baseService.save(domain));
    }

    @PutMapping("/edit")
    public AjaxResult edit(@Validated @RequestBody MesExceptPlanXref domain) {
        return toAjax(baseService.updateById(domain));
    }

    @DeleteMapping("/{pkIds}")
    public AjaxResult remove(@PathVariable Long[] pkIds) {
        baseService.removeBatchByIds(Arrays.asList(pkIds));
        return success();
    }


    /**
     * 新增异常方案关联关系
     */
    /*@Operation(summary = "新增异常方案关联关系")
    @PostMapping("/saveExceptPaln")
    public R saveExceptPaln(@Validated @RequestBody MesExceptPlanXref domain) {
        QueryWrapper<MesExceptPlanXref> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("plan_id", domain.getPlanId());
        queryWrapper.eq("except_id", domain.getExceptId());
        List<MesExceptPlanXref> list = baseService.list(queryWrapper);
        if (list.size() > 0) {
            return R.fail("该方案已经被当前异常关联！");
        }
        //存入关联异常id表
        try {
            baseService.save(domain);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail("保存失败");
        }
        return R.ok();

    }*/

    @Operation(summary = "新增多个异常方案关联关系")
    @PostMapping("/saveExceptPalns")
    public R saveExceptPalns(@Validated @RequestBody MesExceptPlanXref domain) {
        Long[] planIds = domain.getPlanIds();
        Long exceptId = domain.getExceptId();
        if (null != planIds && planIds.length > 0) {
            for (Long planId : planIds) {
                Long count = baseService.lambdaQuery().eq(MesExceptPlanXref::getExceptId, exceptId).eq(MesExceptPlanXref::getPlanId, planId).count();
                if (count > 0) {
                    continue;
                }
                MesExceptPlanXref xref = new MesExceptPlanXref();
                domain.setExceptId(exceptId);
                domain.setPlanId(planId);
                baseService.save(domain);
            }
            return R.ok();
        } else {
            return R.fail("方案不能为空!");
        }
    }
}